package com.lminkey.servlet;

import com.lmonkey.dao.Basedao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/admin/admin_douserdel")
public class DoUserDel extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取数据
        String id = request.getParameter("id");
        //连接数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;
        int count = 0;
        try{
            String sql = "delete from lmonkeyshop where user_id = ? and user_status != 2 ";
            ps = conn.prepareStatement(sql);
            ps.setObject(1,id);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (count>0){
            response.sendRedirect("/lyq/admin/admin_douserselect?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords"));
        }else{
            PrintWriter out = response.getWriter();
            out.println("<script>alert('用户删除失败')location.href='admin_douserselect?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords")+"'</script>");
        }

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取数据
        String ids[] = request.getParameterValues("id[]");
        //连接数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;
        try{
            String sql = "delete from lmonkeyshop where user_name = ? and user_status != 2 ";
            for (int i=0;i<ids.length;i++){
            ps = conn.prepareStatement(sql);
            ps.setObject(1,ids[i]);
            ps.executeUpdate();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
    //跳转页面
        response.sendRedirect("/lyq/admin/admin_douserselect");
    }
}
